package com.ruoyi.web.controller.teaching;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.teaching.domain.TeachingClassesSchedule;
import com.ruoyi.teaching.domain.TeachingStudentApply;
import com.ruoyi.teaching.domain.TeachingStudentRecord;
import com.ruoyi.teaching.service.ITeachingCourseService;
import com.ruoyi.teaching.service.ITeachingStudentApplyService;
import com.ruoyi.teaching.service.ITeachingStudentRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.teaching.domain.TeachingClassesRecord;
import com.ruoyi.teaching.service.ITeachingClassesRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 班级上课记录Controller
 *
 * @author ruoyi
 * @date 2022-10-25
 */
@Controller
@RequestMapping("/teaching/classRecord")
public class TeachingClassesRecordController extends BaseController {
    private String prefix = "teaching/classRecord";

    @Autowired
    private ITeachingClassesRecordService teachingClassesRecordService;

    @Autowired
    private ITeachingCourseService teachingCourseService;

    @Autowired
    private ITeachingStudentRecordService teachingStudentRecordService;

    @Autowired
    private ITeachingStudentApplyService teachingStudentApplyService;

    @Autowired
    private ISysUserService userService;

//    @RequiresPermissions("teaching:classRecord:view")
    @GetMapping()
    public String classRecord() {
        return prefix + "/classRecord";
    }

    /**
     * 查询班级上课记录列表
     */
//    @RequiresPermissions("teaching:classRecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TeachingClassesRecord teachingClassesRecord) {
        startPage();
        teachingClassesRecord.setSchoolId(getSysUser().getSchoolId());
        List<TeachingClassesRecord> list = teachingClassesRecordService.selectTeachingClassesRecordList(teachingClassesRecord);
        for(TeachingClassesRecord classesRecord :list){
            Long courseId = classesRecord.getCourseId();
            classesRecord.setTeachingCourse(teachingCourseService.selectTeachingCourseByCourseId(courseId));
        }
        return getDataTable(list);
    }

    /**
     * 导出班级上课记录列表
     */
//    @RequiresPermissions("teaching:classRecord:export")
    @Log(title = "班级上课记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TeachingClassesRecord teachingClassesRecord) {
        List<TeachingClassesRecord> list = teachingClassesRecordService.selectTeachingClassesRecordList(teachingClassesRecord);
        ExcelUtil<TeachingClassesRecord> util = new ExcelUtil<TeachingClassesRecord>(TeachingClassesRecord.class);
        return util.exportExcel(list, "班级上课记录数据");
    }

    /**
     * 新增班级上课记录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存班级上课记录
     */
//    @RequiresPermissions("teaching:classRecord:add")
    @Log(title = "班级上课记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TeachingClassesRecord teachingClassesRecord) {
        teachingClassesRecord.setCreateId(getUserId());
        teachingClassesRecord.setCreateBy(getLoginName());
        teachingClassesRecord.setCreateTime(new Date());
        teachingClassesRecordService.insertTeachingClassesRecord(teachingClassesRecord);
        return success(teachingClassesRecord);
    }

    /**
     * 修改班级上课记录
     */
//    @RequiresPermissions("teaching:classRecord:edit")
    @GetMapping("/edit/{claRecordId}")
    public String edit(@PathVariable("claRecordId") Long claRecordId, ModelMap mmap) {
        TeachingClassesRecord teachingClassesRecord = teachingClassesRecordService.selectTeachingClassesRecordByClaRecordId(claRecordId);
        mmap.put("teachingClassesRecord", teachingClassesRecord);
        //找出当前账号所在学校的所有老师
        SysUser param = new SysUser();
        param.setSchoolId(getSysUser().getSchoolId());
        param.setUserType("00");
        List<SysUser> teachers = userService.selectUserList(param);
        mmap.put("teachers", teachers);
        return prefix + "/edit";
    }

    /**
     * 修改保存班级上课记录
     */
//    @RequiresPermissions("teaching:classRecord:edit")
    @Log(title = "班级上课记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TeachingClassesRecord teachingClassesRecord) {
        return toAjax(teachingClassesRecordService.updateTeachingClassesRecord(teachingClassesRecord));
    }

    /**
     * 删除班级上课记录
     */
//    @RequiresPermissions("teaching:classRecord:remove")
    @Log(title = "班级上课记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        for (Long claRecordId : Convert.toLongArray(ids)){
            TeachingStudentRecord param = new TeachingStudentRecord();
            param.setClaRecordId(claRecordId);
            List<TeachingStudentRecord> list = teachingStudentRecordService.selectTeachingStudentRecordList(param);
            for (TeachingStudentRecord studentRecord : list){
                Long applyId = studentRecord.getApplyId();
                Long recordId = studentRecord.getRecordId();
                Integer lessonConsume = Convert.toInt(studentRecord.getLessonConsume());
                TeachingStudentApply studentApply = teachingStudentApplyService.selectTeachingStudentApplyByApplyId(applyId);
                Integer lessonRemain = studentApply.getLessonRemain();
                Integer lessonConsume1 = studentApply.getLessonConsume();
                lessonRemain = lessonRemain + lessonConsume;
                lessonConsume1 = lessonConsume1 - lessonConsume;
                studentApply.setLessonRemain(lessonRemain);
                studentApply.setLessonConsume(lessonConsume1);
                teachingStudentApplyService.updateTeachingStudentApply(studentApply);
                teachingStudentRecordService.deleteTeachingStudentRecordByRecordId(recordId);
            }
        }
        teachingClassesRecordService.deleteTeachingClassesRecordByClaRecordIds(ids);
        return success();
    }



    /**
     * 通过claRecordId 进入编辑界面
     */
    @GetMapping("/editModal/{claRecordId}")
    public String editModel(@PathVariable("claRecordId") Long claRecordId, ModelMap mmap) {
        TeachingClassesRecord teachingClassesRecord = teachingClassesRecordService.selectTeachingClassesRecordByClaRecordId(claRecordId);
        mmap.put("teachingClassesRecord", teachingClassesRecord);
        return prefix + "/editModal";
    }
}
